package com.jt.ktdemo.ui.forget

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.jt.ktdemo.R
import com.jt.ktdemo.bean.BackBean
import com.jt.ktdemo.databinding.ActivityForgetThePasswordBinding
import com.jt.ktdemo.http.HTTPAPI
import com.jt.ktdemo.utils.GsonUtils
import com.jt.ktdemo.utils.LogUtil
import com.jt.ktdemo.utils.SpUtils
import com.lzy.okgo.callback.StringCallback
import okhttp3.Call
import okhttp3.Response
import java.lang.Exception

/**
 * 忘记密码去重置密码
 */
public class ForgetThePasswordActivity : AppCompatActivity() {

    private lateinit var binding: ActivityForgetThePasswordBinding
    private var firstEditText: Boolean = false
    private var secondEditText: Boolean = false
    private var thirdEditText: Boolean = false
    private var forthEditText : Boolean = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_forget_the_password)
        binding.activity = this
        initView()
    }

    private fun initView() {
        binding.ivModuleBack.setOnClickListener {
            finish()
        }
        binding.etModuleOriginalPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                var inputStr = s.toString();
                firstEditText = !TextUtils.isEmpty(inputStr)
                checkBoolean()
            }
        })

        binding.etModuleNewPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                var inputStr = s.toString();
                secondEditText = !TextUtils.isEmpty(inputStr)
                checkBoolean()
            }
        })

        binding.etModuleRepeatNewPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                var inputStr = s.toString();
                thirdEditText = !TextUtils.isEmpty(inputStr)
                checkBoolean()
            }
        })

        binding.btModuleRegister.setOnClickListener {
            if (forthEditText) {
                if (!binding.etModuleNewPassword.text.toString().trim().equals(
                        binding.etModuleRepeatNewPassword.text.toString().trim())
                ) {
                    Toast.makeText(this,"input again",Toast.LENGTH_SHORT).show()
                    binding.etModuleNewPassword.setText("")
                    binding.etModuleRepeatNewPassword.setText("")
                }else{
                    updatePwd(binding.etModuleOriginalPassword.text.toString().trim(),
                        binding.etModuleNewPassword.text.toString().trim())
                }
            }
        }
    }

    @SuppressLint("NewApi")
    fun checkBoolean() {
        if (firstEditText && secondEditText && thirdEditText) {
            forthEditText = true
            binding.btModuleRegister.setBackgroundResource(R.drawable.draw_module_login_bg)
        } else {
            forthEditText = false
            binding.btModuleRegister.setBackgroundResource(R.drawable.draw_module_login_bg_selected)
        }
    }

    /**
     * 更新密码
     */
    private fun updatePwd(pwd: String, pwdAgain: String) {
        HTTPAPI.updatePwd(pwd, pwdAgain, object : StringCallback() {
            override fun onSuccess(t: String?, call: Call?, response: Response?) {
                LogUtil.e("密码更新成功-->$t")
                var bean: BackBean = GsonUtils.parse(t.toString(), BackBean::class.java)
                if(!TextUtils.isEmpty(bean.getSession())){
                    SpUtils.put("module_session", bean.getSession())
                }
                Toast.makeText(
                    this@ForgetThePasswordActivity,
                    "Password changed successfully !",
                    Toast.LENGTH_SHORT
                ).show()
                finish()
            }

            override fun onError(call: Call?, response: Response?, e: Exception?) {
                LogUtil.e("密码更新失败-->${e!!.message}")
            }
        })
    }

}